//
//  HJEventkit.m
//  project
//
//  Created by hu on 2019/4/9.
//  Copyright © 2019 健健. All rights reserved.
//

#import "HJEventkit.h"

@interface HJEventkit ()

@property (nonatomic,strong)EKEventStore * eventStore;
@property (nonatomic,strong)EKEvent * ekEvent;
@property (nonatomic,strong)EKAlarm * alarm1;
@property (nonatomic,strong)EKAlarm * alarm2;
@end

@implementation HJEventkit

+(instancetype)shareEventKit {
    static HJEventkit * instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    return instance;
}
-(void)initWithEventKitWithDataDic:(NSDictionary *)dic success:(void(^)(NSString * hudStr))success error:(void(^)(NSString * errorStr))error {
    if (![self calendarAuthority]) {
        error(@"授权失败，请前往设置中授权");
    }else {
        EKEventStore * eventStore = [[EKEventStore alloc]init];
        self.eventStore = eventStore;
        if ([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]) {
            EKEvent * ekEvent = [EKEvent eventWithEventStore:self.eventStore];
            self.ekEvent = ekEvent;
            ekEvent.title = @"你是大傻瓜";
            ekEvent.location = @"我在红旗河沟等你";
            
            NSDateFormatter *tempFormatter = [[NSDateFormatter alloc]init];
            [tempFormatter setDateFormat:@"dd.MM.yyyy HH:mm"];
            
            ekEvent.startDate = [[NSDate alloc]init];
            ekEvent.endDate = [[NSDate alloc]init];
            ekEvent.allDay = YES;
            self.alarm1 = [EKAlarm alarmWithRelativeOffset:60.0f * -60.0f * 24]; //添加一个车闹钟
            self.alarm2 = [EKAlarm alarmWithRelativeOffset:60.0f * -15.0f];
            //添加提醒
            [ekEvent addAlarm:self.alarm1];
            [ekEvent addAlarm:self.alarm2];
            
            [ekEvent setCalendar:[eventStore defaultCalendarForNewEvents]];
            NSError *err;
            [eventStore saveEvent:ekEvent span:EKSpanThisEvent error:&err];
            if (success) {
                success(@"已添加到系统日历中");
            }
            NSLog(@"保存成功");
            
//            EKReminder *reminder = [EKReminder reminderWithEventStore:eventStore];
//            //标题
//            reminder.title = @"";
//            //添加日历
//            [reminder setCalendar:[eventStore defaultCalendarForNewReminders]];
//
//            NSCalendar *cal = [NSCalendar currentCalendar];
//
//            [cal setTimeZone:[NSTimeZone systemTimeZone]];
//
//            NSInteger flags = NSCalendarUnitYear | NSCalendarUnitMonth |
//
//            NSCalendarUnitDay |NSCalendarUnitHour | NSCalendarUnitMinute |
//
//            NSCalendarUnitSecond;
//
//            NSDateComponents* dateComp = [cal components:flags fromDate:self.startDate];
//
//            dateComp.timeZone = [NSTimeZone systemTimeZone];
//
//            reminder.startDateComponents = dateComp; //开始时间
//
//            reminder.dueDateComponents = dateComp; //到期时间
//
//            reminder.priority = 1; //优先级
//
            
//            [reminder addAlarm:alarm];
//            [eventStore saveReminder:reminder commit:YES error:&err];
//            if (err) {
//            }
        }
    }
}

-(void)dealloc {
    [self.ekEvent removeAlarm:self.alarm1];
    [self.ekEvent removeAlarm:self.alarm2];
}
-(BOOL)calendarAuthority {
    __block BOOL isCalendar = false;
    EKAuthorizationStatus eventStatus = [EKEventStore  authorizationStatusForEntityType:EKEntityTypeEvent];
    //用户还没授权过
    if(eventStatus == EKAuthorizationStatusNotDetermined) {
        [[[EKEventStore alloc]init] requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError * _Nullable error) {
            isCalendar = granted;
        }];
    } else if (eventStatus == EKAuthorizationStatusDenied) {//用户授权不允许
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"当前日历服务不可用" message:@"您还没有授权本应用使用日历,请到 设置 > 隐私 > 日历 中授权" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        }];
        [alert addAction:action];
        [[UIViewController currentViewController] presentViewController:alert animated:YES completion:nil];
    }else {
        isCalendar = YES;
    }
    return isCalendar;
}
- (void)showAlert:(NSString *)message {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}
@end
